package com.demo.myapplication.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.customtabs.CustomTabsIntent;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.demo.myapplication.R;
import com.demo.myapplication.bean.ADEntity;
import com.demo.myapplication.ui.ADTextView;
import com.demo.myapplication.utils.CustomTabsHelper;

import java.util.ArrayList;
import java.util.List;

public class CanvasFragment extends Fragment {

  public static CanvasFragment newInstance(Bundle args) {
    CanvasFragment fragment = new CanvasFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_canvas, container, false);
    ADTextView adTv = (ADTextView) view.findViewById(R.id.ad_text);
    adTv.setTexts(initAdText());
    adTv.setOnClickListener(new ADTextView.OnClickListener() {
      @Override
      public void onClick(String mUrl) {
        CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
//        builder.setStartAnimations(getActivity(), R.anim.slide_in_bottom, R.anim.slide_out_top);
//        builder.setExitAnimations(getActivity(), android.R.anim.slide_in_left,
//            android.R.anim.slide_out_right);
        CustomTabsIntent customTabsIntent = builder.build();
        Uri uri = Uri.parse("http://" + mUrl);
        String packageName = CustomTabsHelper.getPackageNameToUse(getActivity());
        if (packageName == null) {
          //no chrome
          startActivity(new Intent(Intent.ACTION_VIEW).setData(uri));
        } else {
          customTabsIntent.intent.setPackage(packageName);
          customTabsIntent.launchUrl(getActivity(), uri);
        }
      }
    });
    return view;
  }

  private List<ADEntity> initAdText() {
    ArrayList<ADEntity> list = new ArrayList<>();
    list.add(new ADEntity("第1", "good morning", "www.baidu.com"));
    list.add(new ADEntity("第2", "我还要上班。。。", "www.v2ex.com"));
    list.add(new ADEntity("第3", "真的是太惨了", "www.zhihu.com"));
    list.add(new ADEntity("第4", "如果可以的话，我就做点别的事吧", "www.qq.com"));
    list.add(new ADEntity("第5", "也许会觉得不那么不爽", "www.acfun.tv"));
    list.add(new ADEntity("第6", "毕竟闲着也是闲着", "www.baidu.com"));
    return list;
  }

}
